﻿/// <reference path="Meres.js" />

$.fn.personSelector = function (options, method, val) {
    var opts = undefined;
    if (options != 'option')
        opts = $.extend({}, $.fn.personSelector.defaults, options);
    var output = undefined;

    this.each(function () {
        output = handlePersonSelectorControl(this);
    });

    return output;

    function handlePersonSelectorControl(input) {
        var $this = $(input);
        if (options == 'option') {
            if (method == 'getSelCategories') {
                var catId = [];
                $this.find(".categories").find('.ui-selected').each(function () {
                    catId.push($(this).attr('data-categoryId'));
                });
                return catId;
            }

            if (method == 'getSelPerson') {
                var pIds = [];
                $this.find(".peopleList").find('.ui-selected').each(function () {
                    pIds.push($(this).attr('data-personid'));
                });
                return pIds;
            }
            if (method == 'setSelPerson') {
                $this.find(".peopleList").find('[data-personId=' + val + ']').each(function () {
                    JsUtil.selectSelectableElements($(this).parent(), this);
                });
            }

            return undefined;
        }


        installjQueryPlugin();
        function installjQueryPlugin() {
            var $tabs = $this.find(".peopleFilter").tabs();
            $tabs.tabs("select", 0);

            $tabs = $this.find(".peopleSelector").tabs();
            $tabs.tabs("select", 0);

            $this.find(".categories").selectable({
                selected: function (sender, args) {
                    var filter = { CategoryId: $(args.selected).attr('data-categoryId') };
                    filterPeople(filter);
                    if (opts.selCategoryChanged != undefined) {
                        opts.selCategoryChanged(filter);
                    }
                }
            });
            //$this.find(".peopleFunctions").selectable();
            $this.find(".peopleSelector .peopleList").selectable(
            {
                selected: function (sender, args) {
                    if (opts.selectedChanged != undefined) {
                        opts.selectedChanged(args);
                    }
                }
            });

            if (opts.allowDragPerson) {
                $this.find('.peopleSelector li').draggable({
                    helper: "clone",
                    appendTo: "body"
                });
            }
        }

        $this.find('.peopleFilterTextBox').text(function (evt) {
            return;
        });

        // #region ui event
        $this.find('.peopleFilterTextBoxClear').click(function () {
            $this.find('.peopleFilterTextBox').attr('value', '');
            doFilter('');
        });

        $('.customtooltip li a').tooltip({ showURL: false });


        function filterPeople(filterCondition) {
            doAjax("/Meres/People/GetPeopleSimple", renderPeople, filterCondition, 'POST');

            function renderPeople(evt) {
                var pList = $this.find('.peopleList');
                pList.html('');
                pList.selectable('destroy');
                for (var i = 0; i < evt.length; i++) {
                    pList.append('<li data-personid="@item.Id">@item.Name</li>'.replace('@item.Id', evt[i].Id).replace('@item.Name', evt[i].Name));
                }

                installjQueryPlugin();
            }
        }

        Messenger.register(Messenger.PERSON_DELETED, function (evt) {
            filterPeople();
        });

        Messenger.register(Messenger.PERSON_ADDED, function (evt) {
            filterPeople();
        });



        jQuery.expr[':'].Contains = function (a, i, m) {
            return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
        };

        filterList();

        function filterList() {
            $this.find('.peopleFilterTextBox').keyup(function () {
                var va = $(this).val();
                doFilter(va);
            });
        }

        function doFilter(filter) {
            if (filter == '') {
                $this.find('.peopleList').find('li').show();
                return;
            }
            $this.find('.peopleList').find('"li:not(:Contains(' + filter + '))').hide();
            $this.find('.peopleList').find('"li:Contains(' + filter + ')').show();
        }
    }
}

$.fn.personSelector.defaults = {
    selectedChanged: undefined,
    selCategoryChanged: undefined,
    allowDragPerson: false
}
